package br.com.rampage.model.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import br.com.rampage.model.bean.Paciente;

public class PacienteDaoImp implements PacienteDao {

	private Session session;

	public void setSession(Session session) {
		this.session = session;
	}

	@Override
	public void salvar(Paciente p) {
		session.save(p);
	}

	@Override
	public void atualizar(Paciente p) {
		session.update(p);
	}

	@Override
	public void excluir(Paciente p) {
		session.delete(p);
	}

	@Override
	public Paciente carregar(String cpf) {
		return (Paciente) session.get(Paciente.class, cpf);
	}

	@Override
	public Paciente buscarPorCpf(String cpf) {
		/*String hql = "select p from Paciente  p where p.cpf = :cpf";
		Query query = session.createQuery(hql);
		query.setString("cpf", cpf);
		return (Paciente) query.uniqueResult();*/
		return (Paciente) session.createCriteria(Paciente.class).add(Restrictions.eq("cpf",cpf)).uniqueResult();
	}

	@Override
	public List<Paciente> listar() {
		return session.createCriteria(Paciente.class).list();
	}
	
	@Override
	public boolean isCadastrado(String cpf) {
		String hql = "select p.cpf from Paciente p where p.cpf = :cpf";
		Query query = session.createQuery(hql);
		query.setString("cpf", cpf);
		if (query.uniqueResult() == null) {
			return false;
		}
		return true;
	}
}
